<?php
/****************************************
  * 设置占位订单定时清除
  *
 ********************************************/
namespace app\commands;

use Yii;
use yii\console\Controller;
use yii\web\NotFoundHttpException;
use app\modules\Product\models\Product;
use app\modules\Order\models\Order;
use app\modules\Order\models\OrderRoom;

class OrderController extends Controller
{

	/**
	* 清除占位订单
	* @param combination  
	*/
    public function actionCheck(){//{{{
		// 获取所有设置占位时间的团期 未发船
		$data = Product::find()->select("id, date_limit")->where(['>','date_limit', 0])->andWhere(['>','go_time', date('Y-m-d')])->all();
		
		foreach($data as $model){
			$t = sprintf("-%d day", $model->date_limit+1);
			$start =  date("Y-m-d 00:00:00",strtotime($t)); //    $model->date_limit;
			$end =  date("Y-m-d 23:59:59", strtotime($t));  //    $model->date_limit;

			// 获取该船期下的所有订单  时间范围内 状态为占位/同业占位
			$orders = Order::find()->where(['product_id'=>$model->id])
								   ->andWhere(['status'=> 1])
								   //->andWhere(['>=', 'created_at',$start])
								   ->andWhere(['<=', 'created_at',$end])
								   ->all();

			foreach($orders as $order){

				// 判断是否已被分团占用
				if(!empty($order->assign_id)){
					echo "订单已分团， 不能取消, 请先在团队中移除该订单"; 	
					continue;
				}

				// 判断是否是关联订单
				if($order->isRelation > 1){
					echo "关联订单, 请先取消关联后操作";	
					continue;
					//return false;
				}

				$order->status = 10; 
				$order->save();      // 改为取消
				// 记录日志

				// 取消订单对应的舱房
				$orderRoom = OrderRoom::updateAll(['status'=>2], sprintf('order_id = %d', $order->id));
				$desc = "取消订单";
				$ret = \Yii::$app->logs->info($order->id, 2, $desc);
				// 取消订单舱房对应的客户 (暂不需要)todo

			}
		}
	}//}}}
	
}
